package com.android.danaobank.adapter;

import android.graphics.Paint;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.danaobank.BuildConfig;
import com.android.danaobank.R;
import com.android.danaobank.model.response.Product;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lg.meng.application.StubApplication;
import com.lg.meng.base.BaseEmptyQuickAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MallProductListAdapter extends BaseEmptyQuickAdapter<Product, BaseViewHolder> implements LoadMoreModule {
    public MallProductListAdapter(@Nullable List<Product> data) {
        super(R.layout.item_mall_product, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, Product product) {
        baseViewHolder.setText(R.id.title, product.getName());
        Glide.with(baseViewHolder.itemView.getContext()).load(BuildConfig.IMAGE_BASE + product.getImgUrl()).into((ImageView) baseViewHolder.getView(R.id.image));
        String saleNum = String.format(StubApplication.getApplication().getString(R.string.sale_num), product.getSaleNum());

        String classifyCode = product.getClassifyCode();
        String salePrice;
        if ("2".equals(classifyCode) && product.getAgentPrice() != product.getSalePrice()) { //折扣价
            salePrice = String.format(StubApplication.getApplication().getString(R.string.price), handlePrice(product.getAgentPrice()));
            baseViewHolder.setVisible(R.id.sale_original_price_tv, true);
            ((TextView) baseViewHolder.getView(R.id.sale_original_price_tv)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            baseViewHolder.setText(R.id.sale_original_price_tv, String.format(StubApplication.getApplication().getString(R.string.price), handlePrice(product.getSalePrice())));
        } else {
            salePrice = String.format(StubApplication.getApplication().getString(R.string.price), handlePrice(product.getSalePrice()));
            baseViewHolder.setGone(R.id.sale_original_price_tv, true);
        }

        baseViewHolder.setText(R.id.sale_price_tv, salePrice);
        baseViewHolder.setText(R.id.sale_num_tv, saleNum);
    }


    private String handlePrice(double price) {
        if (price == (int) price) {
            return (int) price + "";
        }
        return price + "";
    }
}
